# -*- coding: utf-8 -*-
# Copyright 2011-2012 MagiCycles.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from settings import ENABLE_ANONYMOUS_UPLOAD

import people_api
from oauth.utils import create_upload_signature,check_upload_signature_and_delete

def get_upload_signature(request):
    """create a new upload signature and return.
    Args: None
    Returns:
      signature: signature for uploading.
    """
    result = {"status": "error"}
    user = people_api.get_current_user()
    if not user and ENABLE_ANONYMOUS_UPLOAD:
        user = people_api.get_anonymous_user()

    if user:
        result["status"] = "ok"
        result["signature"] = create_upload_signature(user.uid)
    else:
        result["message"] = "you must login first"

    return result

def check_upload_signature(request, signature):
    """check a upload signature and delete it.
    Args:
      signature: signature need to be checked
    Returns:
      status: 'ok' or 'error'
    """
    result = {"status": "error"}
    uid = check_upload_signature_and_delete(signature)
    if uid:
        result["status"] = "ok"
        result["user"] = uid
        result["message"] = "signature check passed and the signature has been deleted"
    else:
        result["message"] = "signature check failed"
    return result

def main():
    pass

if __name__ == '__main__':
    main()
